package com.dxy.yzs.marathon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxy.yzs.marathon.common.R;
import com.dxy.yzs.marathon.domain.Post;
import com.dxy.yzs.marathon.domain.Reply;
import com.dxy.yzs.marathon.service.PostService;
import com.dxy.yzs.marathon.service.ReplyService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * class description
 *
 * @author zhangxunwei
 * @date 2021/10/22
 */
@RestController
@AllArgsConstructor
@RequestMapping("/reply")
public class ReplyController {
    private final ReplyService replyService;
    private final PostService postService;

    @PostMapping("/postReply")
    public R postReply(@RequestBody Map<String, Object> params) {
        Integer type = (Integer) params.get("type");
        Integer targetId = (Integer) params.get("targetId");

        Reply reply = new Reply();
        reply.setContent((String) params.get("content"));
        reply.setType(type);
        reply.setUsername((String) params.get("username"));
        reply.setTargetId(targetId);
        reply.setAvatar((String) params.get("avatar"));

        replyService.save(reply);

        if (type == 0) {
            Post post = postService.getById(targetId);
            if (Objects.nonNull(post)) {
                post.setReplyTimes(post.getReplyTimes() + 1);
                postService.updateById(post);
            }
        }

        return R.ok();
    }
}
